if exists (select 1
          from sysobjects
          where  id = object_id('bd_fn_numToMin')
          and type in ('IF', 'FN', 'TF'))
   drop function bd_fn_numToMin
go


create function bd_fn_numToMin (@min INT)
RETURNS VARCHAR(10)
BEGIN
	IF(ISNULL(@min,0)=0) RETURN '00:00';
  
  DECLARE @result VARCHAR(20)=''
	IF(@min/60<10)
	BEGIN
	SET @result = '0'+CAST(@min/60 AS VARCHAR(20)) + ':' + RIGHT('00'+CAST(@min % 60 AS VARCHAR(20)),2)
	END else
	BEGIN
	SET @result = CAST(@min/60 AS VARCHAR(20)) + ':' + RIGHT('00'+CAST(@min % 60 AS VARCHAR(20)),2)
	END
--   IF(@min<1440)
--   BEGIN
--   	SET @result = +'00:'+RIGHT('00'+CAST(@min/60 AS VARCHAR(20)),2) + ':' + RIGHT('00'+CAST(@min % 60 AS VARCHAR(20)),2)
--   END ELSE
--   BEGIN
--   	SET @result = RIGHT('00'+CAST(@min/1440 AS VARCHAR(20)),2) + ':' + RIGHT('00'+CAST(@min % 1440/60 AS VARCHAR(20)),2) + ':' + RIGHT('00'+CAST(@min % 60 AS VARCHAR(20)),2)
--   END
  
  RETURN @result
END
go
